package com.xiaozhengkeji.minecraft.plugin.fishplus.listener;

import com.xiaozhengkeji.minecraft.plugin.fishplus.FishPlus;
import com.xiaozhengkeji.minecraft.plugin.fishplus.base.MemoryBase;
import com.xiaozhengkeji.minecraft.plugin.fishplus.config.FishConfig;
import com.xiaozhengkeji.minecraft.plugin.fishplus.utils.MsgUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 玩家相关事件的监听
 */
public class PlayerEventListener implements Listener {
    /**
     * 玩家进入服务器
     * 读入数据文件
     */
    @EventHandler(ignoreCancelled = true)
    public void onPlayerJoin(PlayerJoinEvent event) {
        String temp_player_data_path = FishConfig.PATH_STRING_PLAYER_DATA + "/" + event.getPlayer().getUniqueId().toString() + ".yml";
        File temp_player_data_file = new File(temp_player_data_path);
        MemoryBase.PLAYER_DATA_PATH_HASH_MAP.put(event.getPlayer().getUniqueId(), temp_player_data_path);
        if (!temp_player_data_file.exists()) {
            try {
                if (temp_player_data_file.createNewFile()) {
                    MsgUtils.logServer("创建玩家钓鱼数据缓存文件完成!  ->" + temp_player_data_path);
                    if (MemoryBase.initializePlayerData(event.getPlayer().getUniqueId())){
                        MsgUtils.logServer("初始化 玩家数据完成! ");
                        CreateCustomFishListener(event.getPlayer());
                    }else {
                        MsgUtils.logServer("§4初始化 玩家数据失败! ");
                    }
                }
            } catch (IOException e) {
                MsgUtils.logServer("§4创建玩家钓鱼数据缓存文件失败!  ->" + temp_player_data_path);
                e.printStackTrace();
            }
        } else {
            CreateCustomFishListener(event.getPlayer());
        }

    }

    private void CreateCustomFishListener(Player p) {
        PlayerCustomFishListener temp_PlayerCustomFishListener = new PlayerCustomFishListener(p);
        MemoryBase.PLAYER_CUSTOM_FISH_LISTENER_HASH_MAP.put(p.getUniqueId(), temp_PlayerCustomFishListener);
        Bukkit.getPluginManager().registerEvents(temp_PlayerCustomFishListener, FishPlus.Main);
        MsgUtils.logServer("玩家[ " + p.getDisplayName() + " ] 进入服务器 创建 PlayerCustomFishListener 完成!");
    }

    /**
     * 玩家离开服务器
     * 清空缓存
     * 清空监听
     * 清空Map
     */
    @EventHandler(ignoreCancelled = true)
    public void onPlayerQuit(PlayerQuitEvent event) {
        PlayerCustomFishListener temp_PlayerCustomFishListener = MemoryBase.PLAYER_CUSTOM_FISH_LISTENER_HASH_MAP.get(event.getPlayer().getUniqueId());
        HandlerList.unregisterAll(temp_PlayerCustomFishListener);
        MemoryBase.PLAYER_CUSTOM_FISH_LISTENER_HASH_MAP.remove(event.getPlayer().getUniqueId());
        MsgUtils.logServer("玩家[ " + event.getPlayer().getDisplayName() + " ] 离开服务器 移除 PlayerCustomFishListener 完成!");
    }


}
